package ru.paswd.marketprice.utils

import kotlin.math.abs

object NumericUtils {
    private const val ROUBLE_SIGN = "₽"

    fun createDisplayablePrice(price: Int?) =
            createDisplayablePriceWithoutSign(price) + " $ROUBLE_SIGN"

    fun createDisplayablePriceWithoutSign(price: Int?): String {
        if (price == null) {
            return "0"
        }

        val priceRub = price / 100
        val priceDec = price % 100
        return priceRub.toString() + if (priceDec > 0) {
            "." + withExtraZeros(priceDec, 2)
        } else {
            ""
        }
    }

    fun withExtraZeros(num: Int, size: Int): String {
        var result = num.toString()
        val numSize = result.length

        if (numSize < size) {
            val diff = abs(numSize - size)
            for (i in 0..diff) {
                result = "0$result"
            }
        }

        return result
    }
}